var uploadUrl = "http://139.224.12.88:8888/upload/imgUpload"; //外网

define(['jquery-1.8.2.js', 'ajaxfileupload.js'], function(require, exports, module) {
	var U = require('util_module.js');
	var imgType = "";
	var imgFileType = ['png', 'jpg', 'gif', 'bmp', 'PNG', 'JPG', 'GIF', 'BMP', 'JPEG', 'jpeg'];
	/**
	 * 不压缩直接上传
	 */
	function upload(file, config) {
		var formData = new FormData();
		formData.append('img', file);
		U.loadOpt(true);
		$.ajax({
			type: "post", //必须用post  
			url: "http://139.224.12.88:8888/upload/imgUpload",
			data: "imgType=1&image=" + encodeURIComponent(base64),
			crossDomain: true,
			contentType: false, //必须  
			processData: false,
			dataType: "json",
			//不能用success，否则不执行  
			complete: function(data) {
				U.loadOpt(false);
				data = JSON.parse(data.responseText);
				if(data.url.indexOf('上传图片') > -1) {
					config.error(data.url);
				} else {
					if($.isFunction(config.success)) {
						config.success(data.url, config.obj);
					}
				}
			}
		});
	}
	/*
	 * 图片压缩上传
	 */
	function uploadZip(obj, config) {
		console.info(config)
		var dir = $(config.obj).attr("dir");
		var that = obj;
		// 生成比例
		var w = that.width,
			h = that.height,
			scale = w / h;
		w = config.width || w;
		h = w / scale;
		// 生成canvas
		var canvas = document.createElement('canvas');
		var ctx = canvas.getContext('2d');
		$(canvas).attr({
			width: w,
			height: h
		});
		ctx.drawImage(that, 0, 0, w, h);
		/**
		 * 生成base64 
		 */
		var base64 = canvas.toDataURL(imgType, config.quality || 0.8);
		if($($(config.obj)[0]).attr("id") != "temp_upload") {
			U.loadOpt(true);
		}
		$.ajax({
			type: 'post',
			url: 'http://image.pjshfp.com/image/ImageBase64OrdinaryServlet',
			data: {
				'image': base64,
				'type': '1'
			},
			dataType: "json",
			success: function(json) {
				if($($(config.obj)[0]).attr("id") != "temp_upload") {
					U.loadOpt(false);
				}
				if($.isFunction(config.success)) {
					if(json.error == 0) {
						json["state"] = "SUCCESS";
					}
					config.success(json, config.obj);
				}
			}
		});
	}

	function previewImage(file) {}

	function clacImgZoomParam(maxWidth, maxHeight, width, height) {}
	var exports = {
		uploadImg: function(c) {
			var config = {
				'width': 400,
				'quality': 0.8,
				'size': 100
			}; //上传图片默认宽度如果超出400px或者大小超出100kb将进行压缩，清晰比例为0.8
			$.extend(config, c);
			if(U.isIE()) {
				$(config.obj).attr("onchange", previewImage);
			} else {
				config.obj.change(function() {
					var $this = $(this);
					var file = this.files[0];
					imgType = file.type;
					//类型验证
					var type = file.name.substring(file.name.indexOf(".") + 1, file.name.length);
					if($.inArray(type, imgFileType) == -1) {
						if($.isFunction(config.error)) {
							config.error('图片格式需为（PNG,JPG,GIF,BMP,JPEG）');
						}
					} else {
						var URL = window.URL || window.webkitURL;
						var blob = URL.createObjectURL(file);
						var img = new Image();
						img.src = blob;
						img.onload = function() {
							config.obj = $this;
							if(file.size <= config.size * 1024 || this.width <= config.width) {
								uploadZip(this, config);
							} else {
								uploadZip(this, config);
							}
						}
						this.value = ''; // 清空临时数据
					}
				});
			}

		},
		ajaxFileUpload: function(fileId, ajaxUploadCallback) {
			U.loadOpt(true);
			//$("#preloader").css("backgroundColor","transparent");
			//MELLoading.open();
			$("#" + fileId).bind("change", function() {
				if($("#" + fileId).val() == "") {
					alert("请选择图片!");
					//MELLoading.close();
					return;
				}
				$.ajaxFileUpload({
					url: "/upload/" + fileId, //用于文件上传的服务器端请求地址
					type: 'post',
					//data: { "imagetype":imagetype }, //此参数非常严谨，写错一个引号都不行
					secureuri: false, //一般设置为false
					fileElementId: fileId, //文件上传空间的id属性  <input type="file" id="file" name="file" />
					dataType: 'json', //返回值类型 一般设置为json
					success: function(data, status) {
						//MELLoading.close();
						U.loadOpt(false);

						ajaxUploadCallback(data);
					},
					error: function(data, status, e) //服务器响应失败处理函数
						{
							U.loadOpt(false);
							//new MELDialog().openWarn(e);
							//MELLoading.close();
						}
				})
				return false;
			});

		}
	};
	module.exports = exports;
});